from django.db import models

class AIServiceConfig(models.Model):
    name = models.CharField(max_length=255, unique=True, verbose_name="配置名称")
    url = models.URLField(verbose_name="AI服务URL")
    api_key = models.CharField(max_length=255, verbose_name="API密钥")
    is_active = models.BooleanField(default=True, verbose_name="是否启用") 
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "AI服务配置"
        verbose_name_plural = "AI服务配置"

    def __str__(self):
        return self.name

class EmailConfig(models.Model):
    name = models.CharField(max_length=255, unique=True, verbose_name="配置名称")
    email_address = models.EmailField(verbose_name="邮箱地址")
    smtp_server = models.CharField(max_length=255, verbose_name="SMTP服务器")
    smtp_port = models.IntegerField(verbose_name="SMTP端口")
    smtp_username = models.CharField(max_length=255, verbose_name="SMTP用户名")
    smtp_password = models.CharField(max_length=255, verbose_name="SMTP密码")
    use_ssl = models.BooleanField(default=True, verbose_name="使用SSL")
    is_active = models.BooleanField(default=True, verbose_name="是否启用")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "邮箱配置"
        verbose_name_plural = "邮箱配置"

    def __str__(self):
        return self.name

class DingTalkConfig(models.Model):
    name = models.CharField(max_length=255, unique=True, verbose_name="配置名称")
    webhook_url = models.URLField(verbose_name="Webhook URL")
    secret = models.CharField(max_length=255, blank=True, null=True, verbose_name="密钥 (可选)")
    is_active = models.BooleanField(default=True, verbose_name="是否启用")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "钉钉配置"
        verbose_name_plural = "钉钉配置"

    def __str__(self):
        return self.name

class FeishuConfig(models.Model):
    name = models.CharField(max_length=255, unique=True, verbose_name="配置名称")
    app_id = models.CharField(max_length=255, verbose_name="App ID")
    app_secret = models.CharField(max_length=255, verbose_name="App Secret")
    is_active = models.BooleanField(default=True, verbose_name="是否启用")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "飞书配置"
        verbose_name_plural = "飞书配置"

    def __str__(self):
        return self.name

class PushKeyword(models.Model):
    SCHEDULE_CHOICES = [
        ('daily', '每天'),
        ('weekly', '每周'),
        ('monthly', '每月'),
    ]
    keyword = models.CharField(max_length=255, unique=True, verbose_name="推送关键词")
    description = models.TextField(blank=True, null=True, verbose_name="描述")
    manual_text = models.TextField(blank=True, null=True, verbose_name="手动输入文本")
    schedule = models.CharField(max_length=20, choices=SCHEDULE_CHOICES, default='weekly', verbose_name="执行周期")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "推送关键词"
        verbose_name_plural = "推送关键词"

    def __str__(self):
        return self.keyword

class PushTask(models.Model):
    TASK_TYPE_CHOICES = [
        ('email', '邮箱推送'),
        ('dingtalk', '钉钉推送'),
        ('feishu', '飞书推送'),
    ]
    keyword = models.ForeignKey(PushKeyword, on_delete=models.CASCADE, verbose_name="关键词")
    task_type = models.CharField(max_length=20, choices=TASK_TYPE_CHOICES, verbose_name="推送类型")
    email_config = models.ForeignKey(EmailConfig, on_delete=models.SET_NULL, null=True, blank=True, verbose_name="邮箱配置")
    dingtalk_config = models.ForeignKey(DingTalkConfig, on_delete=models.SET_NULL, null=True, blank=True, verbose_name="钉钉配置")
    feishu_config = models.ForeignKey(FeishuConfig, on_delete=models.SET_NULL, null=True, blank=True, verbose_name="飞书配置")
    schedule_time = models.DateTimeField(verbose_name="执行时间")
    is_active = models.BooleanField(default=True, verbose_name="是否启用")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "推送任务"
        verbose_name_plural = "推送任务"

    def __str__(self):
        return f"{self.keyword.keyword} - {self.get_task_type_display()} ({self.schedule_time.strftime('%Y-%m-%d %H:%M')})"

class PushHistory(models.Model):
    task = models.ForeignKey(PushTask, on_delete=models.CASCADE, verbose_name="推送任务")
    status = models.CharField(max_length=50, verbose_name="状态") # e.g., 'success', 'failed'
    result_message = models.TextField(blank=True, null=True, verbose_name="结果消息")
    executed_at = models.DateTimeField(auto_now_add=True, verbose_name="执行时间")

    class Meta:
        verbose_name = "推送历史"
        verbose_name_plural = "推送历史"

    def __str__(self):
        return f"{self.task.keyword.keyword} - {self.status} at {self.executed_at.strftime('%Y-%m-%d %H:%M')}"
